#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

/**
 * Definition for singly-linked list.
*/
  struct ListNode {
      int val;
      struct ListNode *next;
  };
 
struct ListNode* addTwoNumbers_(struct ListNode* l1, struct ListNode* l2) {
	struct ListNode *h=NULL,*lp=NULL;
	struct ListNode **r=&h;
	int add=0,tmp;
	while( l1 || l2 || add ) {
		*r=(struct ListNode*)malloc(sizeof(struct ListNode));
		lp=*r;
		tmp=add;
		if( l1 && l2 ){
			tmp=l1->val+l2->val+tmp;
			l1=l1->next,l2=l2->next;
		}
		else if( l1 ){
			tmp=l1->val+tmp;
			l1=l1->next;
		}
		else if( l2 ){
			tmp=l2->val+tmp;
			l2=l2->next;
		}
		else{}
		if( tmp >= 10 ){
			tmp=tmp%10;
			add=1;
		}else
			add=0;
		lp->val=tmp;
		lp->next=NULL;
		r=&(lp->next);
	}  
	return h;
}


struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
	struct ListNode *h=NULL,*lp=NULL;
	struct ListNode **r=&h;
	int add=0,tmp;
	while( l1 && l2 ){
		*r=(struct ListNode*)malloc(sizeof(struct ListNode));
		lp=*r;
		tmp=add;
		tmp+=l1->val+l2->val;
		if( tmp >= 10 ){
			add=1;
			tmp=tmp%10;
		}else
			add=0;
		lp->val=tmp;
		lp->next=NULL;
		r=&(lp->next);
		l1=l1->next;
		l2=l2->next;
	}
	while( l1 ){
		*r=(struct ListNode*)malloc(sizeof(struct ListNode));
		lp=*r;
		tmp=add;
		tmp+=l1->val;
		if( tmp >= 10 ){
			add=1;
			tmp=tmp%10;
		}else
			add=0;
		lp->val=tmp;
		lp->next=NULL;
		r=&(lp->next);
		l1=l1->next;
	}
	while( l2 ){
		*r=(struct ListNode*)malloc(sizeof(struct ListNode));
		lp=*r;
		tmp=add;
		tmp+=l2->val;
		if( tmp >= 10 ){
			add=1;
			tmp=tmp%10;
		}else
			add=0;
		lp->val=tmp;
		lp->next=NULL;
		r=&(lp->next);
		l2=l2->next;
	}
	if( add ){
		*r=(struct ListNode*)malloc(sizeof(struct ListNode));
		lp=*r;
		tmp=add;
		lp->val=1;
		lp->next=NULL;
	}
	return h;
}
int main(){

	return 0;
}
